package edu.boun.g4.coursity.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.boun.g4.coursity.domain.Course;
import edu.boun.g4.coursity.domain.Institution;

/**
 * 
 * @author mertusta
 * @author bulutkorkmaz
 * @author alifindik
 * 
 */
@Service("institutionService")
@Transactional
public class InstitutionService {

	protected static Logger logger = Logger.getLogger("service");

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * Retrieves all institutions
	 * 
	 * @return a list of institutions
	 */
	@SuppressWarnings("unchecked")
	public List<Institution> getAll() {
		logger.debug("Retrieving all institutions");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  Institution");

		// Retrieve all
		return query.list();
	}

	/**
	 * Retrieves institution of a course
	 */
	public Institution getAll(Integer courseId) {
		logger.debug("Retrieving institution of the course");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session
				.createQuery("FROM Course as p LEFT JOIN FETCH  p.institution WHERE p.id="
						+ courseId);

		Course course = (Course) query.uniqueResult();

		// Retrieve all
		return course.getInstitution();
	}

	/**
	 * Retrieves a single institution
	 */
	public Institution get(Integer id) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing institution first
		Institution institution = (Institution) session.get(Institution.class,
				id);

		return institution;
	}

	/**
	 * Retrieves a single Institution by its info
	 */
	public Institution getByInfo(String info) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM Institution WHERE info='"
				+ info + "'");

		Institution institution = (Institution) query.uniqueResult();

		return institution;
	}

	/**
	 * Adds a new institution
	 */
	public void add(Institution institution) {
		logger.debug("Adding new institution");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Persists to db
		session.save(institution);
	}

	/**
	 * Deletes an existing institution
	 * 
	 * @param id
	 *            the id of the existing institution
	 */
	public void delete(Integer id) {
		logger.debug("Deleting existing institution");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing institution first
		Institution institution = (Institution) session.get(Institution.class,
				id);

		// Delete
		session.delete(institution);
	}

	/**
	 * Edits an existing initiative
	 */
	public void edit(Institution institution) {
		logger.debug("Editing existing institution");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing institution via id
		Institution existingInstitution = (Institution) session.get(
				Institution.class, institution.getId());

		// Assign updated values to this institution
		existingInstitution
				.setInstitutionName(institution.getInstitutionName());
		existingInstitution.setInfo(institution.getInfo());

		// Save updates
		session.save(existingInstitution);
	}
}
